<?php
namespace Api\Controller;
use Think\Controller;
class TaskController extends Controller{
	public function index(){
		
	}
	
	//用户层
		//发布任务
	public function releaseTask(){
		$task = M("Task");
		$task->user_id = $_POST["user_id"];
		$task->Task_num = $_POST["Task_num"];
		$task->j_type = $_POST['j_type'];
		$typelist=serialize($_POST['j_typelist']);
		$task->j_typelist = $typelist;
		$task->j_price = $_POST['j_price'];
		$task->j_time = strtotime($_POST['j_time']);
		$task->j_address = $_POST['j_address'];
		$task->j_content = $_POST['j_content'];
		$task->lat = $_POST['lat'];
		$task->lng = $_POST['lng'];
		$task->create_time = time();
		
		if($task->add()){
			$this->noticeNewTask();
			$value = ["release" => "success"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ["release" => "wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		
	}
		//展示用户的任务
	public function showUserTasks(){
		$status = $_REQUEST["status"]; //3中状态，发布untaked，接单taked，结算finish
		$user_id = $_REQUEST["user_id"];
		$task = M("Task");
		//$data = $task->where("user_id = $user_id and status = '$status'")->select();
		
		if($status == "untaked"){
			$sql="select o.* from task as o  where o.user_id = $user_id and o.status = '$status'";
			//未接单就只是显示订单信息
			$data = $task->query($sql);
		}elseif($status == "taked"){
			//接单后要显示接单的安装工信息
			$sql ="select o.*,u.mobile as erectormobile from task as o left join user as u on o.erector_id = u.id where o.user_id = $user_id and o.status = '$status'";
			$data = $task->query($sql);
			
		}elseif($status == "finish"){
			//完成后要显示所有相关信息，
			$sql ="select o.*,u.mobile as erectormobile from task as o left join user as u on o.erector_id = u.id where o.user_id = $user_id and o.status = '$status'";
			$data = $task->query($sql);
		}else{
			$sql="select o.* from task as o  where o.user_id = $user_id and o.status = '$status'";
			//未接单就只是显示订单信息
			$data = $task->query($sql);
		}
		if($status !="untaked"){
			for($i=0;$i<count($data);$i++){
				$erector_id = $data[$i]["erector_id"];
				if($erector_id) {
					$name = M("Erector")->where("user_id = $erector_id")->field("realname")->find()["realname"];
					$data[$i]["erectorname"] = $name;
				}
				
			}
		}
		$value = ["code"=>"success","data"=>$data];
		header("Content-Type: text/json");
		die(json_encode($value));
	}
		//取消任务
	public function cancelTask(){
		$task_id = $_POST["task_id"];
		$task = M("Task");
		
		$task->status = "cancel";
		if($task->where("id = $task_id")->save()){
			$value = ["cancel" => "success"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ["cancel" => "wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
		//删除订单
	public function deleteTask(){
		$task = M("Task");
		$task->id = $_POST["task_id"];
		if($task->delete()){
			$value = ["delete" => "success"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ["delete" => "wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
		//发布成功的提醒
	public function noticeNewTask(){
		
	}	
	
		//接单成功后的通知
	public function noticeUser(){
		$task_id= $_POST["task_id"];
		$tasks = M("Task")->where("id = $task_id")->find();
		$user_id = $tasks['user_id'];
		$erector_id = $tasks["erector_id"];
		$user = M("User")->where("id = $user_id")->field("username,mobile")->find();
		$erector = M("Erector")->join("user on user.id = erector.user_id")->where("id = $user_id")->field("erector.realname,user.mobile")->find();
		$mobile  = $user["mobile"];
		$data = "你好，你发布的任务".$tasks['j_type']."已被接单，安装工:".$erector["realname"]."联系电话：".$erector["mobile"];
		sendSms($mobile, $data);
	}
	
	public function checkOnlink(){
		
	}
	//安装工层
		//显示任务
	public function showErectorTasks(){
		$status = $_REQUEST["status"]; //3中状态，接单taked，完成finish unpay 明细表finish pay
		$erector_id = $_REQUEST["erector_id"];
		
		//分页设置
		$pagecount=isset($_REQUEST['pagecount'])?$_REQUEST['pagecount']:"10";//每页显示数量
		$pagenum=$pagecount*((isset($_REQUEST['pagenum'])?$_REQUEST['pagenum']:"1")-1);//当前页数
		
		$task = M("Task");
		
		
		if($status == "taked"){
			//接到的单
			$sql="select o.*,u.username,u.mobile from  task as o left join user as u on o.user_id = u.id where erector_id = $erector_id and o.status = '$status'";
			$data = $task->query($sql);
		}elseif($status == "finish"){
			//已完成的
			$sql="select o.*,u.username,u.mobile from  task as o left join user as u on o.user_id = u.id where erector_id = $erector_id and o.status = '$status' and pay_status = 'unpay'";
			$data = $task->query($sql);
		}elseif($status == 'pay'){
			//已支付的
			$sql="select o.*,u.username,u.mobile from  task as o left join user as u on o.user_id = u.id where erector_id = $erector_id and o.status = 'finish' and pay_status = 'pay'";
			$data = $task->query($sql);
		}
		$value=["code"=>"success","data"=>$data];
		header("Content-Type: text/json");
		die(json_encode($value));
		
	}

		//显示附近的任务
	public function showNearTasks(){
		
		$lat=isset($_POST['lat'])?$_POST['lat']:"111.86141967773438";
		$lng=isset($_POST['lng'])?$_POST['lng']:"33.07078170776367";
		$distance = $_POST["distance"];
		$where ="";
		if(isset($_POST['type'])){
			$where = "and j_type = '".$_POST["type"]."'";
		}
		$task = M("Task");
		$sql="select o.*,u.username,u.mobile,(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*($lat-lat)/360),2)+COS(PI()*$lng/180)* COS(lat * PI()/180)*POW(SIN(PI()*($lng-lng)/360),2)))) as distance from task as o left join user as u on o.user_id = u.id where o.status  = 'untaked' and  $distance >= (2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*($lat-lat)/360),2)+COS(PI()*$lng/180)* COS(lat * PI()/180)*POW(SIN(PI()*($lng-lng)/360),2))))".$where;
		$data = $task->query($sql);
		if(is_array($data)){
			$value = ["code" => "success","data" => $data];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ["code" => "wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		
		
	}
	
		//接单
	public function receiveTask(){
		$task = M("Task");
		$id = $_POST["task_id"];
	
		$ta=$task->where("id = $id")->find();
		if($ta["status"] != "untaked"){
			$value = ["take" => "already"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		
		$task ->erector_id = $_POST["erector_id"];
		$task ->id = $id;
		$task ->status = "taked";
		$task ->take_time = time();
		
		if($task->where("id = $id")->save()){
			$value = ["take" => "success"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ["take" => "wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
		//任务完成
	public function finishTask(){
		
		$erector_id = $_POST["erector_id"];
		$id = $_POST["task_id"];
		$task = M("Task");
		$task->startTrans();
		$price = $task->where("id = $id")->field("j_price")->find()["j_price"];
		$task ->erector_id = $erector_id;
		
		$task ->status = "finish";
		$task ->finish_time = time();
		if($task->where("id = $id")->save()){
			
			$erector = M("Erector");
			$e = $erector->where("user_id = $erector_id")->find();
			$money = $e['money'] + $price;
			$erector->money = $money;
			
			if($erector->where("user_id = $erector_id")->save()){
				$task->commit();
				$value = ["finish" => "success"];
				header("Content-Type: text/json");
				die(json_encode($value));
			}else{
				$task->rollback();
				$value = ["finish" => "wrong"];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
			
		}else{
			$value = ["finish" => "wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
		
		//搜索功能
		/*
		 * 搜索任务，
		 * 条件：家具类型，安装时间*/
	public function selectTasks(){
		$sele = $_POST["sele"];
		
				
	}
	

	public function updateTask(){
		$time=time();
		$ta =M("Task");
		$ta->status = "cancel";
		$ta->where("status = 'untaked' and j_time <$time")->save();

	}
	
	public function reflesh(){
		$task_id = $_POST["task_id"];
		if(M("Task")->where("id = $task_id")->find()["status"] != "untaked"){
			$value = ["refresh" => "now"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}

	
}